package dao

import (
	"fmt"
	"zhihu/inits"
	"zhihu/model"
)

func AddText(title string, content string) bool {
	result := &model.Text{
		Title:   title,
		Content: content,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return false
	}
	return true
}
func LikeCount(textId int, count int) int {
	count = count + 1
	inits.DB.Model(&model.Text{}).Where("id=?", textId).Update("like_count", count)
	return count
}
func LookLog(textId int, count int) int {

	count = count + 1
	inits.DB.Model(&model.Text{}).Where("id=?", textId).Update("look_log", count)
	return count
}
func ListText(textId int, likeCount int, lookLog int) (result *model.Text) {
	if err := inits.DB.Where("id=?", textId).Find(&result).Error; err != nil {
		return nil
	}

	count := LikeCount(textId, likeCount)
	log := LookLog(textId, lookLog)
	fmt.Println(count, log)
	return result
}
func AddReview(textId int, context string) bool {
	result := &model.Review{
		TextId:  textId,
		Context: context,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return false
	}
	return true
}
func AddReviewByReview(textId int, context string, fatherId int) bool {
	result := &model.Review{
		TextId:   textId,
		Context:  context,
		FatherId: fatherId,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return false
	}
	return true
}
func UpdateLikeCount(ReviewId int, count int) int {
	count = count + 1
	inits.DB.Model(&model.Review{}).Where("id=?", ReviewId).Update("like_count", count)
	return count
}
func ListReview(ReviewId int, likeCount int) (result *model.Review) {
	if err := inits.DB.Where("id=?", ReviewId).Order("like_count asc").Find(&result).Error; err != nil {
		return nil
	}
	count := UpdateLikeCount(ReviewId, likeCount)
	fmt.Println(count)
	return result
}
